/**
 * @author 小泽玛丽罗
 */

/**
 * @return{TimeMMDDHHMMSS}
 */
function TimeNow(){
		var myDate=new Date();
		var hours=myDate.getHours();
		var min=myDate.getMinutes()+1;     //获取当前分钟数(0-59)    
		var sec=myDate.getSeconds();
		//var years=myDate.getYear();
		var month=myDate.getMonth()+1;
		var date=myDate.getDate();
		var time=month+"-"+date+" "+hours+":"+min+":"+sec;
		return time;
}

/**
 * HTML5 localStorge.getItem 函数的封装
 * @param {Object} key
 */
function getLocalStorge(key){
	var item = localStorage.getItem(key);
	if(item=='' || item=='0' || item==null || typeof(item)=="undefined"){
		return false;
	}else{
		return item;
	}
}

	
/**
 * @argumen 用于生成友好时间
 * @param obj 时间戳
 * @returnes obj  带友好时间的对象
 */
function addFriendlyTime(obj){
	var da=obj;
	da=parseInt(da)*1000;
	da=new Date(da);
	var year=da.getFullYear();
	var month=da.getMonth()+1;
	var date=da.getDate();
	//var time=da.Date();
	var nowDate=new Date();
	if(year!=nowDate.getFullYear())
	{
		str=year+'-'+month+'-'+date;
		return str;
	}	
	else
	{
		var monthin=nowDate.getMonth()-month+1;
		if(monthin==0)
		{
			dayin=nowDate.getDate()-date;
			if(dayin==0)
			{
				return da.getHours()+':'+da.getMinutes();
			}
			else if(dayin==1)
			{
				return "昨天"+da.getHours()+':'+da.getMinutes();
			}
			else
			{	
				var m=parseInt(da.getMonth())+1;
				return m+'-'+da.getDate();
			}
		}
		else{
			var m=parseInt(da.getMonth())+1;
			
			return m+'-'+da.getDate();
		}
	}
}
function addFriendlyTime2(timestampInPage){
	var timestampNow = Math.round(new Date().getTime()/1000);
   var dif = timestampNow-timestampInPage;
   var outPut ="";

   if (dif<=15) outPut ="刚才";
   else if(dif<60) outPut=dif+"秒前";
   else if(dif<3600) outPut=Math.round(dif/60)+"分钟前";
   else if(dif<86400) outPut=Math.round(dif/3600)+"小时前";
   else {
       var date = new Date(timestampInPage*1000);
       var hours = date.getHours();
       var minutes =date.getMinutes();
       if(hours<10) hours = "0"+hours;
       if(minutes<10) minutes="0"+minutes;

       outPut = date.getMonth()+1+"-"+(date.getDate()+1)+" "+hours+":"+minutes;
   }
   return outPut;
}
/**
 * 将对象objS的图片路径提到objF的第一层上
 * @param {Object} objF
 * @param {Object} objS
 */
function picSrcUpLevel(objF,objS){
	var len=objS.length;
	for(var i=0;i<len;i++){
		objF["pic"+i]=objS[i].small;
		
	}
	return objF;
}
/**
 * @argument 将father中son对象内的元素提到与son同级
 * @param {Object} father
 * @param {Object} son
 */


function userToArt(father,son){
	father["uid"]=son.uid;
	father["username"]=son.nickname;
	var headUrl=son.avatar_url;
	if(headUrl=="http://tm.ourstu.com/Addons/Avatar/default.jpg")
			{
				father["HeadPic"]="css/image/default.png";
			}
			else{
				father["HeadPic"]=son.avatar_url;
			}
	return father;
}

/**
 * 
 * @param {Object} count
 * @param {Object} obj
 * @param {Object} comment
 */

function comment_user_ToArt(count,obj,comment){
	if(count==1)
	{
		obj["comment_content"]=obj["comment"][0].content;
		var item=comment[0];
		obj["comment_username"]=item["user"].username;
		return obj;
	}
	else
	{
		for(var item in comment)
		{	
			item=comment[item];
			obj["comment_content1"]=item.content;
			obj["comment_username1"]=item.user.username;
			obj["comment_content2"]=item.content;
			obj["comment_username2"]=item.user.username;
		}
		return obj;
	}
}

/**
 * @argument 用户加载微博
 * @param {string} type 例如 Weibo At Comment Collect
 */
function loadWeiboList(type){
		var httpID=11011;
		if(type=="Weibo"){
			var url='http://www.diaoyumi.ourstu.com/api/weibo/getFollowingWeiboList?session_id='+window.localStorage["session_id"];//+'&offset=0'+'&count=10&comment_count=5';
		}else if(type=="Comment"){
			var url='http://www.diaoyumi.ourstu.com/api/weibo/getCommentMeList?session_id='+window.localStorage["session_id"];//+'&offset=0'+'&count=10&comment_count=5';
		}else if(type=="At"){
			var url='http://www.diaoyumi.ourstu.com/api/weibo/getAtMeWeiboList?session_id='+window.localStorage["session_id"];
		}else{
			var url='http://www.diaoyumi.ourstu.com/api/weibo/getBookmarkList?session_id='+window.localStorage["session_id"];
		}
		
		
		uexXmlHttpMgr.onData=function(inOpCode,inStatus,inResult){
			if(inStatus==1){
				var json=eval("(" + inResult + ")");
				uexXmlHttpMgr.close(httpID);
				if(json["success"]){
					
					var time=TimeNow();
					//设置本次刷新时间,refreshTime+type例如refreshTimeWeibo,refreshTimeAt.refreshTimeComment ,refreshTimeCollect
					window.localStorage.setItem("refreshTime"+type,time);
					//设置本次刷新时间,offset+type例如offsetWeibo,offsetAt,offsetComment,offsetCollect
					var len=zy_tmpl_count(json["list"]);
					window.localStorage.setItem(type+"offset",len);//点击加载时从第几条开始的参数
					var i=0;
					for( i=0;i<len;i++)
					{	
						var obj=json["list"][i];
						obj=userToArt(obj,obj.user);
						//obj=UrlAdd(obj);	
						obj["ctime"]=addFriendlyTime2(obj.ctime);
						
						obj=WeiboJsonDataHandle(type,obj);
						json["list"][i]=obj;
					}
					window.localStorage[type+"WeiboList"]=JSON.stringify(json);//将json对象转换成json字符串保存
					//var read=window.localStorage[type+"WeiboList"];
					var add=document.createElement('DIV');
					var end=zy_tmpl_s(tmpl_add,obj,zy_tmpl_count(obj));
					add.innerHTML=end;
					
					document.body.appendChild(add);
					
				}else{
					uexWindow.toast(1,5,json["message"],500);
				}
			}
		}
		uexXmlHttpMgr.open(httpID,'get',url,3000);
		uexXmlHttpMgr.send(httpID);
}
/**
 * @argument  用于根据微博类型加载微博
 * @param {Object} type 例如 Weibo At Comment Collect
 * @param {Object} obj  一个微博实体
 */
function WeiboJsonDataHandle(type,obj){
	if(type=="Comment"){
			var d=document.createElement('DIV');
			var obj=upCommentedWeiboContentLevel(obj,obj.weibo);
			var weibo=zy_tmpl_s(tmpl_forward,obj,zy_tmpl_count(obj));
			d.innerHTML=weibo;
			document.body.appendChild(d);
			return obj;
		}else {//先判断是不是转发的
			if(obj["is_forward"]){//微博是转发的
				var forward_weibo=obj["forward_weibo"];
				if(forward_weibo["picture_list"].length!=0){//再判断转发的微博有么有图片
					obj=picSrcUpLevel(obj,forward_weibo.picture_list);
					var d=document.createElement('DIV');
					if(obj["is_digg"]){
						obj["digg_pic"]="css/img/like.png";
					}else{
						obj["digg_pic"]="css/img/like1.png";
					}
					
					obj=forwardContentUpLevel(obj,obj.forward_weibo);
					var weibo=zy_tmpl_s(tmpl_forward_pic,obj,zy_tmpl_count(obj));
								
					d.innerHTML=weibo;
					document.body.appendChild(d);
					return obj;
					
				}else{//转发的微博没有图片
					var d=document.createElement('DIV');
					if(obj["is_digg"]){
						obj["digg_pic"]="css/img/like.png";
					}else{
						obj["digg_pic"]="css/img/like1.png";
					}
					obj=forwardContentUpLevel(obj,obj.forward_weibo);
					var weibo=zy_tmpl_s(tmpl_forward,obj,zy_tmpl_count(obj));
								
					d.innerHTML=weibo;
					document.body.appendChild(d);
					return obj;
				}
			}else{//微博不是转发的
				if(obj["picture_list"].length!=0){//微博有图片
					obj=picSrcUpLevel(obj,obj.picture_list);
					var d=document.createElement('DIV');
					if(obj["is_digg"]){
						obj["digg_pic"]="css/img/like.png";
					}else{
						obj["digg_pic"]="css/img/like1.png";
					}
					//obj=forwardContentUpLevel(obj,obj.forward_weibo);
					var weibo=zy_tmpl_s(tmpl_Noforward_Pic,obj,zy_tmpl_count(obj));
								
					d.innerHTML=weibo;
					document.body.appendChild(d);
					return obj;
				}else{//微博无图片
					var d=document.createElement('DIV');
					if(obj["is_digg"]){
						obj["digg_pic"]="css/img/like.png";
					}else{
						obj["digg_pic"]="css/img/like1.png";
					}
					//obj=forwardContentUpLevel(obj,obj.forward_weibo);
					var weibo=zy_tmpl_s(tmpl_Noforward,obj,zy_tmpl_count(obj));
								
					d.innerHTML=weibo;
					document.body.appendChild(d);
					return obj;
				}
			}
		}
		
}
/**
 * @argument 将转发的微博内容提到微博实体第一级
 * @param {Object} objF
 * @param {Object} objS
 */
function forwardContentUpLevel(objF,objS){
	objF["forward_content"]=objS["content"];
	return objF;
}
/**
 * 
 * @param {Object} objF
 * @param {Object} objS
 * @return {Object} obj 
 */
function upCommentedWeiboContentLevel(objF,objS){
	objF["CommentedWeibo_Content"]=objS["content"];
	objF["CommentedWeibo_id"]=objS["weibo_id"];
	return objF;
}
/**
 * @argument 给微博内容中的url加上a标签
 * @return 
 */
function UrlAdd(obj){
	var url=obj["content"].split("http");
	obj["content"]=url[0]+'<a href="http'+url[1]+'">';
	return obj;
}

/**
 * @argument 用户点击加载微博 
 * @param {String} type 例如 Weibo At Comment
 */
function clickAdd(obj){
	var type=obj.getAttribute("name");
	if(type=="Weibo"){
			var url='http://www.diaoyumi.ourstu.com/api/weibo/getFollowingWeiboList';
		}else if(type=="Comment"){
			var url='http://www.diaoyumi.ourstu.com/api/weibo/getCommentMeList';
		}else if(type=="At"){
			var url='http://www.diaoyumi.ourstu.com/api/weibo/getAtMeWeiboList';
		}else if(type=="PerWeibo"){
			var url='http://www.diaoyumi.ourstu.com/api/weibo/getWeiboList';
		}
		else{
			var url='http://www.diaoyumi.ourstu.com/api/weibo/getBookmarkList';
		}
	if(type=="PerWeibo"){
		clickAddLoadPerWeibo(url,type);
	}else{
		clickAddLoadWeibo(url,type);
	}	
}
/**
 * 加载微博列表中的微博 不是个人中心的微博
 * @param {Object} url
 * @param {Object} type
 */
function clickAddLoadWeibo(url,type){
	var btn_add=$$("add");
	btn_add.innerHTML="正在加载";
	var httpID=110;
	var offset=type+"offset";
	uexXmlHttpMgr.onData=function(inOpCode,inStatus,inResult){
		if(inStatus==1){
			var json=eval("("+inResult+")");
			if(json["success"])
			{
				var len=zy_tmpl_count(json.list);
				
				window.localStorage[offset]=parseInt(window.localStorage[offset])+parseInt(len);
				var i=0;
				for( i=0;i<len;i++)
				{	
					var obj=json["list"][i];
					obj=userToArt(obj,obj.user);
					//obj=UrlAdd(obj);	
					obj["ctime"]=addFriendlyTime2(obj.ctime);
					obj=WeiboJsonDataHandle(type,obj);
					json["list"][i]=obj;
				}
				uexWindow.closeToast();
				window.localStorage[type+"WeiboList"]=JSON.stringify(json);//将json对象转换成json字符串保存
				//var read=window.localStorage[type+"WeiboList"];
				var add=document.createElement('DIV');
				var end=zy_tmpl_s(tmpl_add,obj,zy_tmpl_count(obj));
				add.innerHTML=end;
				document.body.appendChild(add);
				
			}
		}
	}
	uexXmlHttpMgr.open(httpID,'post',url,3000);
	var offsets=window.localStorage[offset];
	uexXmlHttpMgr.setPostData(httpID,'0',"offset",offsets);
	uexXmlHttpMgr.setPostData(httpID,'0','session_id',window.localStorage["session_id"]);
	uexXmlHttpMgr.send(httpID);
	uexWindow.toast(1,5,'加载中',0);
	btn_add.parentNode.removeChild(btn_add);
}
/**
 * 加载个人中的微博
 * @param {Object} url
 */
function clickAddLoadPerWeibo(url,type){
	var btn_add=$$("add");
	btn_add.innerHTML="正在加载";
	var httpID=110;
	var offset=type+"offset";
	uexXmlHttpMgr.onData=function(inOpCode,inStatus,inResult){
		if(inStatus==1){
			var json=eval("("+inResult+")");
			if(json["success"])
			{
				var len=zy_tmpl_count(json.list);
				
				window.localStorage[offset]=parseInt(window.localStorage[offset])+parseInt(len);
				var i=0;
				for( i=0;i<len;i++)
				{	
					var obj=json["list"][i];
					obj=userToArt(obj,obj.user);
					//obj=UrlAdd(obj);	
					obj["ctime"]=addFriendlyTime2(obj.ctime);
					obj=WeiboJsonDataHandle(type,obj);
					json["list"][i]=obj;
				}
				uexWindow.closeToast();
				window.localStorage[type+"WeiboList"]=JSON.stringify(json);//将json对象转换成json字符串保存
				//var read=window.localStorage[type+"WeiboList"];
				var add=document.createElement('DIV');
				var end=zy_tmpl_s(tmpl_add,obj,zy_tmpl_count(obj));
				add.innerHTML=end;
				document.body.appendChild(add);
				
			}
		}
	}
	uexXmlHttpMgr.open(httpID,'post',url,3000);
	var offsets=window.localStorage[offset];
	uexXmlHttpMgr.setPostData(httpID,'0','uid','1');
	uexXmlHttpMgr.setPostData(httpID,'0','session_id',window.localStorage["session_id"]);
	uexXmlHttpMgr.send(httpID);
	uexWindow.toast(1,5,'加载中',0);
	btn_add.parentNode.removeChild(btn_add);
}
/**
 * @argument 发布微博
 */
function publicWeibo(){
	uexWindow.open("dymsendweibo",0,"dymsendweibo.html",1,"","",0,250);
}

/**
 * 
 * @param {Object} type 例如forward转发 public发布微博  comment评论微博
 * @param {Object} weiboId 要评论的微博Id
 * @param {Object} txtId  评论内容的id
 */
function publicWeiboAll(type,weiboId,txtId){
	if(type=="forward"){
		var httpID=111;
		uexXmlHttpMgr.onData=function(inOpCode,inStatus,inResult){
			if(inStatus==1){
				var json=JSON.parse(inResult);
				if(json["success"]){
					uexWindow.toast(1,5,"转发成功。",500);
					uexXmlHttpMgr.close();
					//uexWindow.open("dymsendweibo",0,"dymsendweibo.html",1,"","",0,250);
					
				}else{
					uexWindow.toast(1,5,json["message"],500);
				}
			}
		}
		uexXmlHttpMgr.open(httpID,'post',url,3000);
		uexXmlHttpMgr.setPostData(httpID,"session_id",window.localStorage["session_id"]);
		uexXmlHttpMgr.setPostData(httpID,"weibo_id",window.localStorage["weibo_id"]);
		uexXmlHttpMgr.send(httpID);
	}
	else if(type=="public"){
		
	}
	else if(type=="comment"){
		var httpID=112;
		uexXmlHttpMgr.onData=function(inOpCode,inStatus,inResult){
			if(inStatus==1){
				var json=JSON.parse(inResult);
				if(json["success"]){}
				uexWindow.toast(1,5,"评论成功。",500);
				uexXmlHttpMgr.close();
				//uexWindow.open("dymsendweibo",0,"dymsendweibo.html",1,"","",0,250);
				
			}else{
				uexWindow.toast(1,5,json["message"],500);
			}
		}
		var txt=$$("txtId").value;
		uexXmlHttpMgr.open(httpID,'post',url,3000);
		uexXmlHttpMgr.setPostData(httpID,"session_id",window.localStorage["session_id"]);
		uexXmlHttpMgr.setPostData(httpID,"weibo_id",window.localStorage["comment_weibo_id"]);
		uexXmlHttpMgr.setPostData(httpID,"content",txt);
		uexXmlHttpMgr.send(httpID);
	}
	
}
/**
 * @argument 点赞
 * @param {Object} obj
 */
function digg(obj){
		var id=obj.id;
		
		var url='http://www.diaoyumi.ourstu.com/api/weibo/digg?session_id='+window.localStorage["session_id"]+'&weibo_id='+id;
		var httpID=1121;
		uexXmlHttpMgr.onData=function (inOpCode,inStatus,inResult){
			if(inStatus==1){
				var json=eval("("+inResult+")");
				if(json["success"]){
					$$(id).src="css/img/like.png";
					uexWindow.toast(1,5,"点赞成功",500);
				}else{
					uexWindow.toast(1,5,json["message"],500);
				}
			}
		}
		uexXmlHttpMgr.open(httpID,'get',url,3000);
		uexXmlHttpMgr.send(httpID);
	}
	/**
	 *  加载微博详情页
	 * @param {Object} obj
	 */
function weiboDetail(obj){
	var parent=obj.parentNode;
	window.localStorage.setItem("weiboDetail_id",parent.id);
	uexWindow.open("weiboDetail",0,"weiboDetail.html",1,"","",0,400);
	
}

/**
 * @argument 加载微博详情
 */
 function loadWeiboDetail(){
 	var weibo_id=window.localStorage["weiboDetail_id"];
	var url='http://www.diaoyumi.ourstu.com/api/weibo/getWeibo?session_id='+window.localStorage["session_id"]+'&weibo_id='+weibo_id;
	var httpID=13134;
	uexXmlHttpMgr.onData=function(inOpCode,inStatus,inResult){
		if(inStatus==1){
			var json=eval("("+inResult+")");
			if(json["success"]){
				obj=json["weibo"];
				obj=userToArt(obj,obj.user);
				obj["ctime"]=addFriendlyTime2(obj.ctime);
				window.localStorage.setItem("is_collect",json["is_bookmark"]);
				var head=document.createElement('DIV');	
				var weibo=zy_tmpl_s(tmpl_head,obj,zy_tmpl_count(obj));//微博详情页的头部加载		
				head.innerHTML=weibo;
				document.body.appendChild(head);
				if(obj["is_digg"]){
					obj["digg_pic"]="css/img/like.png";
				}else{
					obj["digg_pic"]="css/img/like1.png";
				}
				var pic_len=obj["picture_list"].length;
				
				if (obj["picture_list"].length!= 0) {//判断微博是否是有图片
					obj=picSrcUpLevel(obj,obj.picture_list);
					
					var content=document.createElement('DIV');
					var weibo=zy_tmpl_s(tmpl_content_noForward_pic,obj,zy_tmpl_count(obj));		
					content.innerHTML=weibo;
					document.body.appendChild(content);
					//加载评论
					if(obj["comment_count"]!=0){
						var com_len=obj["comment_count"];
						for(var j=0;j<com_len;j++){
							var uobj=obj["comment_list"][j];
							uobj["ctime"]=addFriendlyTime2(uobj.ctime);
							uobj=userToArt(uobj,uobj.user);
							var comment=document.createElement('DIV');
							var comcon=zy_tmpl_s(tmpl_comment,uobj,zy_tmpl_count(obj));
							comment.innerHTML=comcon;
							document.body.appendChild(comment);	
						}
					}
				}
				else{
					if(obj["is_forward"]){
						if(obj.forward_weibo["picture_list"].length!=0){
							obj=picSrcUpLevel(obj,obj.forward_weibo.picture_list);
							
							var content=document.createElement('DIV');
							var weibo=zy_tmpl_s(tmpl_content_noForward_pic,obj,zy_tmpl_count(obj));		
							content.innerHTML=weibo;
							document.body.appendChild(content);
							//加载评论
							if(obj["comment_count"]!=0){
								var com_len=obj["comment_count"];
								for(var j=0;j<com_len;j++){
									var uobj=obj["comment_list"][j];
									uobj["ctime"]=addFriendlyTime2(uobj.ctime);
									uobj=userToArt(uobj,uobj.user);
									var comment=document.createElement('DIV');
									var comcon=zy_tmpl_s(tmpl_comment,uobj,zy_tmpl_count(obj));
									comment.innerHTML=comcon;
									document.body.appendChild(comment);	
								}
							}
						}else{
							obj["forward_content"]=obj["forward_weibo"].content;
							var content=document.createElement('DIV');
							var weibo=zy_tmpl_s(tmpl_content_forward,obj,zy_tmpl_count(obj));		
							content.innerHTML=weibo;
							document.body.appendChild(content);
							//加载评论
							if(obj["comment_count"]!=0){
								var com_len=obj["comment_count"];
								for(var j=0;j<com_len;j++){
									var uobj=obj["comment_list"][j];
									uobj["ctime"]=addFriendlyTime2(uobj.ctime);
									uobj=userToArt(uobj,uobj.user);
									var comment=document.createElement('DIV');
									var comcon=zy_tmpl_s(tmpl_comment,uobj,zy_tmpl_count(obj));
									comment.innerHTML=comcon;
									document.body.appendChild(comment);	
								}
							}
						}
						
					}else{
						var content=document.createElement('DIV');
						var weibo=zy_tmpl_s(tmpl_content_noForward,obj,zy_tmpl_count(obj));		
						content.innerHTML=weibo;
						document.body.appendChild(content);
					}
					//加载评论
					if(obj["comment_count"]!=0){
						var com_len=obj["comment_count"];
						for(var j=0;j<com_len;j++){
							var uobj=obj["comment_list"][j];
							uobj["ctime"]=addFriendlyTime2(uobj.ctime);
							uobj=userToArt(uobj,uobj.user);
							var comment=document.createElement('DIV');
							var comcon=zy_tmpl_s(tmpl_comment,uobj,zy_tmpl_count(obj));
							comment.innerHTML=comcon;
							document.body.appendChild(comment);	
						}
							
					}
				}
				
				
			}else{
				uexWindow.toast(1,5,json["message"],500);
			}
		}
	}
	uexXmlHttpMgr.open(httpID,'get',url,3000);
	uexXmlHttpMgr.send(httpID);
 }
 
 //下拉刷新部分的函数
 
 function set_bounce(){
	uexWindow.setBounce("1");
    setUpstyle("最后一次刷新时间:xxxx");
    uexWindow.showBounceView("0","rgba(142,201,96,30)","1");
    uexWindow.showBounceView("1","rgba(142,201,96,30)","0");
    uexWindow.notifyBounceEvent('0','1');
    uexWindow.onBounceStateChange = function(type,state){
      if(type == '0' && state == "2"){
	  	//alert("1");
		 loadData();
      } 
	  //alert("2");
	 
    }
}

function refreshClear(){
		var divAll=$("body>div");
		divAll.remove();
		
	}
	
	 function loadData(){
    //uexWindow.toast(1,5,"loading....",2000);
    //2秒后，恢复弹动初始状态
	refreshClear();
	//alert("4");
	loadWeiboList("Weibo");
	//alert("5");
    setTimeout(function(){uexWindow.resetBounceView('0');},1500);
  }
   function setUpstyle(ltext){
    var json_p = '{"imagePath":"res://refresh_upicon.png","textColor":"#FFF","levelText":"'+"最后一次刷新时间"+window.localStorage["refreshTimeWeibo"]+'","pullToReloadText":"下拉可以刷新","releaseToReloadText":"松开即可刷新","loadingText":"正在刷新中..."}';
    uexWindow.setBounceParams('0',json_p);
	//alert("3");
  }